package com.rcloud.server.sealtalk.manager;

import com.github.binarywang.java.emoji.EmojiConverter;
import com.rcloud.server.sealtalk.constant.ErrorCode;
import com.rcloud.server.sealtalk.controller.param.UserCollectParam;
import com.rcloud.server.sealtalk.domain.UserCollect;
import com.rcloud.server.sealtalk.exception.ServiceException;
import com.rcloud.server.sealtalk.model.dto.UserCollectDTO;
import com.rcloud.server.sealtalk.service.UserCollectService;
import com.rcloud.server.sealtalk.service.UsersService;
import com.rcloud.server.sealtalk.util.N3d;
import com.rcloud.server.sealtalk.util.ValidateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author: Jianlu.Yu
 * @Date: 2020/8/11
 * @Description:
 * @Copyright (c) 2020, rongcloud.cn All Rights Reserved
 */
@Slf4j
@Service
public class CollectManager extends BaseManager {

    @Resource
    private UsersService usersService;

    @Resource
    private UserCollectService collectService;


    /**
     * 查询用户收藏列表
     *
     * @param type
     * @param userId
     * @return
     */
    public List<UserCollectDTO> queryCollectList(Integer type, Integer userId) {
        log.info("查询用户收藏列表. type:[{}] userId:[{}]", type, userId);
        return collectService.queryCollectList(type, userId);
    }

    /**
     * 获取用户的常用语列表信息
     * @param userId
     * @return
     */
    public List<UserCollectDTO> queryCommonWordsList(Integer userId) {
        log.info("获取用户的常用语列表信息.  userId:[{}]", userId);
        return collectService.queryCommonWordsList(userId);
    }

    /**
     * 添加用户收藏
     *
     * @param param  参数信息
     * @param userId 当前登陆用户ID
     */
    public void saveCollect(UserCollectParam param, Integer userId) throws ServiceException {
        log.info("添加用户收藏. param:[{}] userId:[{}]", param.toString(), userId);
        String name = param.getName();
        EmojiConverter emojiConverter = EmojiConverter.getInstance();
        String content = emojiConverter.toAlias(param.getContent());
        Integer type = param.getType();
        Integer touid = N3d.decode(param.getTouid());
        ValidateUtils.notEmpty(name);
        ValidateUtils.notEmpty(content);
        ValidateUtils.notNull(type);
        ValidateUtils.notNull(touid);

        //封装用户参数信息
        UserCollect build = UserCollect.builder()
                .name(name)
                .content(content)
                .type(type)
                .touid(touid)
                .createBy(userId)
                .createTime(new Date())
                .updateTime(new Date())
                .build();

        if (type == 1 || type == 2 || type == 7) {
            build.setIsCommonWords(1);
        } else {
            build.setIsCommonWords(0);
        }
        collectService.saveSelective(build);
    }

    /**
     * 编辑用户收藏
     */
    public void editCollect(UserCollectParam param) throws ServiceException {
        log.info("编辑用户收藏.id:[{}]", param.getId());
        UserCollect collect = collectService.getByPrimaryKey(param.getId());
        if (collect == null) {
            throw new ServiceException(ErrorCode.BUSI_USER_COLLECT_QUERY_ERROR);
        }
        EmojiConverter emojiConverter = EmojiConverter.getInstance();
        String content = emojiConverter.toAlias(param.getContent());
        UserCollect build = UserCollect.builder()
                .id(param.getId())
                .name(param.getName())
                .content(content)
                .updateTime(new Date())
                .type(param.getType())
                .build();
        //封装用户参数信息
        collectService.updateByPrimaryKeySelective(build);
    }

    /**
     * 删除用户收藏
     */
    @Transactional
    public void removeCollect(List<Integer> ids) throws ServiceException {
        log.info("删除用户收藏.ids:[{}]", ids);
        for (Integer id : ids) {
            UserCollect collect = collectService.getByPrimaryKey(id);
            if (collect == null) {
                throw new ServiceException(ErrorCode.BUSI_USER_COLLECT_QUERY_ERROR);
            }
            //封装用户参数信息
            collectService.deleteByPrimaryKey(id);
        }
    }

    /**
     * 设置用户收藏信息为常用语
     */
    @Transactional
    public void setCollectCommonWords(List<Integer> ids, Integer setWordsType) throws ServiceException {
        log.info("设置用户收藏信息.ids:[{}]", ids);
        for (Integer id : ids) {
            UserCollect collect = collectService.getByPrimaryKey(id);
            if (collect == null) {
                throw new ServiceException(ErrorCode.BUSI_USER_COLLECT_QUERY_ERROR);
            }
            if (collect.getType() == 1 || collect.getType() == 2  || collect.getType() == 7 ) {
                if (setWordsType == 1) {
                    collect.setIsCommonWords(1);
                } else {
                    collect.setIsCommonWords(0);
                }
            }else {
                throw new ServiceException(ErrorCode.BUSI_SET_USER_COLLECT_NOT_SUPPORT_ERROR);
            }

            //封装用户参数信息
            collectService.updateByPrimaryKeySelective(collect);
        }
    }


}